package com.mondo.financialwallet.data.net;


import com.mondo.financialwallet.BuildConfig;
import com.mondo.financialwallet.data.entity.BasisResponseEntity;
import com.mondo.financialwallet.data.entity.CheckMessageCodeResultEntity;
import com.mondo.financialwallet.data.entity.ClearProductDetailsEntity;
import com.mondo.financialwallet.data.entity.FeedbackResponseEntity;
import com.mondo.financialwallet.data.entity.FindEntity;
import com.mondo.financialwallet.data.entity.GoogleValidatorEntity;
import com.mondo.financialwallet.data.entity.HoldProductDetailsEntity;
import com.mondo.financialwallet.data.entity.HoldProductListEntity;
import com.mondo.financialwallet.data.entity.HoldProductProfitDataEntity;
import com.mondo.financialwallet.data.entity.LoginUserInfoEntity;
import com.mondo.financialwallet.data.entity.MobilePrefixEntity;
import com.mondo.financialwallet.data.entity.MyTokenListEntity;
import com.mondo.financialwallet.data.entity.ProductDetailsEntity;
import com.mondo.financialwallet.data.entity.ProductListEntity;
import com.mondo.financialwallet.data.entity.ProductOrderListEntity;
import com.mondo.financialwallet.data.entity.SingleHoldProductProfitDataEntity;
import com.mondo.financialwallet.data.entity.TokenAddressEntity;
import com.mondo.financialwallet.data.entity.TokenTransactionRecordEntity;
import com.mondo.financialwallet.data.entity.UserAssetsEntity;
import com.mondo.financialwallet.data.entity.UserInfoEntity;
import com.mondo.financialwallet.data.entity.VersionUpdateEntity;
import com.mondo.financialwallet.data.entity.WalletAssetEntity;
import com.mondo.financialwallet.data.params.FeedbackParams;

import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Part;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface WebService {

    /**
     * 发现DApp
     */
    @GET("Notice/findings")
    Call<FindEntity> findDAppList();

    /**
     * 获取用户信息
     */
    @GET("user")
    Call<UserInfoEntity> userInfo();

    /**
     * 用户资产
     */
    @GET("asset")
    Call<UserAssetsEntity> userAssets();

    /**
     * 我的数字货币
     */
    @GET("wallet/list")
    Call<MyTokenListEntity> myTokenList();

    /**
     * 钱包资产
     */
    @GET("wallet/asset")
    Call<WalletAssetEntity> walletAsset();

    /**
     * 钱包资产余额
     */
    @GET("wallet/available_asset")
    Call<WalletAssetEntity> walletAssetAvailable();

    /**
     * 产品列表
     */
    @GET("goods")
    Call<ProductListEntity> productList();

    /**
     * 获取手机区号
     */
    @GET("mobile_prefix")
    Call<MobilePrefixEntity> mobilePrefix();

    /**
     * 检测更新
     */
    @GET("notice/check_updates")
    Call<VersionUpdateEntity> versionUpdate();

    /**
     * 理财订单列表
     */
    @GET("order/index_app")
    Call<ProductOrderListEntity> productOrderList();

    /**
     * 查看谷歌验证器
     */
    @GET("google/get_google_code")
    Call<GoogleValidatorEntity> googleValidator();

    /**
     * 持有产品列表
     */
    @GET("order/get_order_list")
    Call<HoldProductListEntity> holdProductList();

    /**
     * 持有产品收益数据
     */
    @GET("profit")
    Call<HoldProductProfitDataEntity> holdProductProfitData();

    /**
     * 获取单个产品收益数据
     */
    @GET("profit/{orderNumber}")
    Call<SingleHoldProductProfitDataEntity> singleHoldProductProfitData(
            @Path("orderNumber") String orderNumber
    );

    /**
     * 修改头像
     */
    @Multipart
    @POST("user/avatar")
    Call<BasisResponseEntity> modifyAvatar(@Part MultipartBody.Part avatar);

    /**
     * 持有产品详情
     */
    @GET("fund/{orderNumber}")
    Call<HoldProductDetailsEntity> holdProductDetails(@Path("orderNumber") String orderNumber);

    /**
     * 清算数据
     */
    @GET("clear/{orderNumber}")
    Call<ClearProductDetailsEntity> clearProductDetails(@Path("orderNumber") String orderNumber);

    /**
     * 用户反馈
     */
    @Headers({"Content-Type: application/json","Accept: application/json"})
    @POST("api/v1/tickets?sign=1b626cb3d27e1610c081c1fc0f826f00")
    Call<FeedbackResponseEntity> customerFeedback(@Body FeedbackParams params);

    /**
     * 清算产品
     */
    @FormUrlEncoded
    @POST("clear")
    Call<BasisResponseEntity> clearProduct(
            @Field("order_no") String orderNumber, @Field("code") String code
    );

    /**
     * 绑定验证器
     */
    @FormUrlEncoded
    @POST("google")
    Call<BasisResponseEntity> bindGoogleValidator(@Field("code") String code);

    /**
     * 获取钱包地址
     */
    @GET("wallet_address/get")
    Call<TokenAddressEntity> tokenAddress(@Query("token_id") String tokenId);

    /**
     * 发送短信
     */
    @FormUrlEncoded
    @POST("user/send_code_by_username")
    Call<BasisResponseEntity> sendMessageByUser(@Field("value") String mobileNumber);

    /**
     * 检测短信验证码
     */
    @FormUrlEncoded
    @POST("user/check_code_by_username")
    Call<CheckMessageCodeResultEntity> checkMessageCodeResult(
            @Field("username") String mobileNumber, @Field("code") String code
    );

    /**
     * 产品详情
     */
    @GET("goods/{id}")
    Call<ProductDetailsEntity> productDetails(@Path("id") String id);

    /**
     * 修改用户昵称
     */
    @FormUrlEncoded
    @POST("user/nickname")
    Call<BasisResponseEntity> alterNickname(@Field("nickname") String nickname);

    /**
     * 修改密码
     */
    @FormUrlEncoded
    @POST("user/change_password")
    Call<BasisResponseEntity> alterPassword(
            @Field("password") String password, @Field("username") String mobileNumber,
            @Field("token") String token, @Field("code") String code
    );

    /**
     * 修改登录密码
     */
    @FormUrlEncoded
    @PUT("user/1")
    Call<BasisResponseEntity> alterLoginPassword(
            @Field("old_password") String oldPassword, @Field("new_password") String newPassword,
            @Field("code") String code
    );

    /**
     * 转账
     */
    @FormUrlEncoded
    @POST("wallet/transfer")
    Call<BasisResponseEntity> transfer(
            @Field("token_id") String tokenId, @Field("number") double number,
            @Field("to") String address, @Field("remark") String remark
    );

    /**
     * 生成理财订单
     */
    @FormUrlEncoded
    @POST("wallet/financing")
    Call<BasisResponseEntity> generateFinancingOrders(
            @Field("goods_id") String goodsId, @Field("usd_amount") String amount
    );

    /**
     * 发送短信验证码
     */
    @FormUrlEncoded
    @POST("user/send_code")
    Call<BasisResponseEntity> sendMessageCode(
            @Field("type") int type, @Field("mobile_prefix") CharSequence prefix,
            @Field("value") CharSequence value
    );

    /**
     * 注册
     */
    @FormUrlEncoded
    @POST("user")
    Call<BasisResponseEntity> register(
            @Field("invite_id") CharSequence inviteId, @Field("code") CharSequence code,
            @Field("mobile") CharSequence mobile, @Field("password") CharSequence password,
            @Field("type") int type, @Field("mobile_prefix") CharSequence prefix,
            @Field("email") CharSequence email
    );

    /**
     * 绑定新手机
     */
    @FormUrlEncoded
    @POST("user/bind_mobile")
    Call<BasisResponseEntity> bindNewMobileNumber(
            @Field("mobile") String mobile, @Field("code") String code,
            @Field("mobile_prefix") String prefix
    );

    /**
     * 绑定邮箱
     */
    @FormUrlEncoded
    @POST("user/bind_email")
    Call<BasisResponseEntity> bindEmail(@Field("email") String email, @Field("code") String code);

    /**
     * 登录
     */
    @FormUrlEncoded
    @POST("user/mobile_login")
    Call<LoginUserInfoEntity> login(
            @Field("mobile_prefix") CharSequence prefix, @Field("mobile") CharSequence mobile,
            @Field("password") CharSequence password
    );

    /**
     * Token交易记录
     */
    @GET("wallet_history/list")
    Call<TokenTransactionRecordEntity> tokenTransactionRecord(
            @Query("page_index") int index, @Query("page_size") int size, @Query("income") String income,
            @Query("token_id") String tokenId
    );

    // 客服服务器
    class CustomerService extends Basis {
        private static WebService mInstance;

        public static WebService get() {
            if (mInstance == null) {
                synchronized (CustomerService.class) {
                    if (mInstance == null) {
                        new CustomerService();
                    }
                }
            }

            return mInstance;
        }

        CustomerService() {
            Retrofit mRetrofit = new Retrofit.Builder()
                    .client(okhttpclient())
                    .baseUrl(BuildConfig.CUSTOMER_SERVICE_DOMAIN)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            mInstance = mRetrofit.create(WebService.class);
        }
    }

    // 钱包服务器
    class Token extends Basis {
        private static WebService mInstance;

        public static WebService get() {
            if (mInstance == null) {
                synchronized (Token.class) {
                    if (mInstance == null) {
                        new Token();
                    }
                }
            }

            return mInstance;
        }

        Token() {
            Retrofit mRetrofit = new Retrofit.Builder()
                    .client(okhttpclient())
                    .baseUrl(BuildConfig.TOKEN_DOMAIN)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            mInstance = mRetrofit.create(WebService.class);
        }
    }

    // 默认服务器
    class Default extends Basis{
        private static WebService mInstance;

        public static WebService get() {
            if (mInstance == null) {
                synchronized (Default.class) {
                    if (mInstance == null) {
                        new Default();
                    }
                }
            }

            return mInstance;
        }

        Default() {
            Retrofit mRetrofit = new Retrofit.Builder()
                    .client(okhttpclient())
                    .baseUrl(BuildConfig.DEFAULT_DOMAIN)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            mInstance = mRetrofit.create(WebService.class);
        }
    }

    class Basis {

        OkHttpClient okhttpclient() {
            HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
            logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            return new OkHttpClient.Builder()
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .addInterceptor(new HeaderInterceptor())
                    .addNetworkInterceptor(logInterceptor)
                    .build();
        }

        static RequestBody toRequestBody(String value) {
            if (value != null && !"".equals(value)) {
                return null;
            }

            return RequestBody.create(MediaType.parse("text/plain"), value);
        }

        static RequestBody toRequestBody(int value) {
            return toRequestBody(String.valueOf(value));
        }

        static RequestBody toRequestBody(double value) {
            return toRequestBody(String.valueOf(value));
        }

        static RequestBody toRequestBody(boolean value) {
            return toRequestBody(String.valueOf(value));
        }
    }
}